<template>
  <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>

    <el-table :data="familyForm" border style="width: 100%">
      <el-table-column prop="relation" label="关系" width="120">
        <template slot-scope="scope">
          <el-select v-model="scope.row.relationType" placeholder="请选择关系" clearable size="small">
            <el-option
              v-for="dict in dict.relation_type"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
              v-if="dict.dictValue != '10'"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="customName" label="姓名" width="180">
        <template slot-scope="scope">
          <custom-filterable
            @cv="(v)=>changeCustomName(v, scope.$index)"
            allowCreate
            optionValue="customName"
            placeholder="请输入姓名"
            :value="scope.row.customName"
          />
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="身份证">
        <template slot-scope="scope">
          <el-input v-model="scope.row.idCard"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="cellPhoneNumber" label="手机号">
        <template slot-scope="scope">
          <el-input v-model="scope.row.cellPhoneNumber"></el-input>
        </template>
      </el-table-column>
      <el-table-column width="260" label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete" size="mini"
            @click="handleFamilyDelete(scope.row, scope.$index)"
            v-hasPermi="['property:member:remove']"
            v-if="scope.$index != 0"
          >
            删除
          </el-button>
          <el-button type="success" plain icon="el-icon-delete" size="mini" @click="handleFamilySave(scope.row)">{{
            scope.row.save ? '修改' : '保存' }}
          </el-button>
          <el-button v-if="scope.$index == familyForm.length - 1" type="primary" plain icon="el-icon-plus" size="mini"
                     @click="handleFamilyAdd" v-hasPermi="['property:member:add']">
            增加
          </el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-dialog>
</template>

<script>
  import { roomCustomList,listCustomInfo,addFamily,delCustomInfo } from "@/api/waitress/customInfo";
  import CustomFilterable from "@/views/components/filterable/CustomFilterable"

  export default {
    name: "familyList",
    dicts:[ 'relation_type' ],
    components:{ CustomFilterable },
    data() {
      return {
        title: '业主成员管理',
        open: false,
        familyForm: [],
        currentlyOption:{}
      }
    },
    methods: {
      show(option){
        this.open = true
        this.currentlyOption = option
        this.getList()
      },
      getList(){
        let params = {
          roomId: this.currentlyOption.roomId,
          params:{
            searchQuery:"[{ matchType:'AND' , propName:'relationType' ,queryType:'NE',value:'10',column:{},alias:'t'}]"
          }
        }
        roomCustomList(params).then(res=>{
          this.familyForm = res.data
          if(this.familyForm.length < 1){
            this.handleFamilyAdd()
          }
        })
      },
      handleFamilyAdd() {
        let obj = {
          roomId: this.currentlyOption.roomId,
          customId: null,
          ownerType: '1002', //业主类型 家庭成员
          relationType: '',
          customName: '',
          idCard: '',
          wrVerify:'N',
          cellPhoneNumber: '',
        };
        this.familyForm.push(obj);
      },
      handleFamilySave(row){
        if(!row.customName){
          this.msgWarning("姓名不能为空")
          return
        }
        if(!row.cellPhoneNumber){
          this.msgWarning("手机号码不能为空")
          return
        }
        addFamily(row).then(res=>{
          this.msgSuccess("保存成功")
          this.getList()
        })
      },
      changeCustomName(v,index){
        listCustomInfo({customName : v}).then(res=>{
          if(res.rows.length > 0){
            this.familyForm[index].customId = res.rows[0].customId
            this.familyForm[index].customName = res.rows[0].customName
            this.familyForm[index].idCard = res.rows[0].idCard
            this.familyForm[index].cellPhoneNumber = res.rows[0].cellPhoneNumber
          }else{
            this.familyForm[index].customId = null
            this.familyForm[index].customName = v
            this.familyForm[index].idCard = null
            this.familyForm[index].cellPhoneNumber = null
          }
        })
      },
      handleFamilyDelete(row,index){
        this.familyForm.splice(index,1)
        if(row.customId){
          delCustomInfo(row.customId).then(res=>{})
        }
      }
    }
  }
</script>

<style scoped>

</style>
